<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use Redis;
class Index extends controller
{
	
	
	//测试swoole
	 public function swoole()
    {
		phpinfo();die;
		//创建Server对象，监听 127.0.0.1:9501端口
		$serv = new swoole_server("127.0.0.1", 9501);
		//监听连接进入事件
		$serv->on('connect', function ($serv, $fd) {
			echo "Client: Connect.\n";
		});
	}
	
	
	
	//首页
    public function index()
    {
		$redis = new Redis();
		$redis->connect('127.0.0.1',6379);
		echo 'yes';
		//return $this->fetch();
		/*
		$num = 900;
		$n = 0;
		while ($n<$num) {
			$n++;
			$map['num'] = $n;
			$map['title'] = '测试';
			DB::name('cheshi')->insertGetId($map);
		}
		*/
		/*
		$array = DB::name('cheshi')->select();
		$map['title'] = '测试002';
		foreach($array as $k=>$v){
			$a = DB::name('cs')->where('num',$v['num'])->find();
			if($a){
				DB::name('cs')->where('id',$a['id'])->update($map);
			}else{
				$map['num'] = $v['num'];
				DB::name('cs')->insertGetId($map);
			}
			//DB::name('cheshi')->where('id',$v['id'])->update($map);
		}
		dump($array);
		die;
		*/
		/*
        return '<style type="text/css">*{ padding: 0; margin: 0; } .think_default_text{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p> ThinkPHP V5<br/><span style="font-size:30px">十年磨一剑 - 为API开发设计的高性能框架</span></p><span style="font-size:22px;">[ V5.0 版本由 <a href="http://www.qiniu.com" target="qiniu">七牛云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ad_bd568ce7058a1091"></think>';
		*/
	}
	
	
	
	//@妖孽 2018/12/05 11:25:06
	/**
     * [asyncExecute PHP异步执行任务]
     * @param  string $url       执行任务的url地址
     * @param  array  $post_data 需要post提交的数据POST
     * @param  array  $cookie    cookie数据用于登录等的设置(此处内部调用，无需鉴权)
     * @return boole
     */
	 
    public function asyncExecute($url, $post_data = array()) {
        $method = "GET";
        $url_array = parse_url($url);
        $port = isset($url_array['port']) ? $url_array['port'] : 80;

        $fp = fsockopen($url_array['host'], $port, $errno, $errstr, 30);
        if (!$fp) {
            return FALSE;
        }
        $getPath = isset($url_array['path']) ? $url_array['path'] : '/';
        if (isset($url_array['query'])) {
            $getPath .= "?" . $url_array['query'];
        }
        if (!empty($post_data)) {
            $method = "POST";
        }
        $header = $method . " /" . $getPath;
        $header .= " HTTP/1.1\r\n";
        $header .= "Host: " . $url_array['host'] . "\r\n";

        $header .= "Connection: Close\r\n";
//        if (!empty($cookie)) {
//            $_cookie = strval(NULL);
//            foreach ($cookie as $k => $v) {
//                $_cookie .= $k . "=" . $v . "; ";
//            }
//            $cookie_str = "Cookie: " . base64_encode($_cookie) . " \r\n";
//            $header .= $cookie_str;
//        }
        if (!empty($post_data)) {
            $_post = strval(NULL);
            $atComma = '';
            foreach ($post_data as $k => $v) {
                $_post .= $atComma . $k . "=" . $v;
                $atComma = '&';
            }
            $post_str = "Content-Type: application/x-www-form-urlencoded\r\n";
            $post_str .= "Content-Length: " . strlen($_post) . "\r\n";
            $post_str .= "\r\n".$_post . "\r\n";
            $header .= $post_str;
        }
        $header .= "\r\n";
        $re = fwrite($fp, $header);
        fclose($fp);
        return $re;
    }
	
	
	
	
	
	
	
	
}
